package co.fitstart.admin.entity.search;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.persistence.support.QueryBuilder;

/**
 * 
 * @author lidan
 *
 */
public class OnlineStudentSearch implements QueryBuilder {
    
    private String name;
    
    private String mobile;
    
    private String type;
    
    private Order order;
    
    private Filter filter;
    
    public OnlineStudentSearch() {
       order = new Order("student.createTime", "DESC");
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public Filter getPreparedFilter(boolean refresh) {
        
        
        
        if(filter == null || refresh) {
            
            filter = new Filter().addLike("student.name", name)
                                 .addLike("student.mobile", mobile);
            
            if(StringUtils.equals(type, "online")) {
                filter.addGreaterThan("student.stat.onlineCount", 0);
            } else {
                filter.addSentence("student.stat.individualCount+student.stat.groupCount+student.stat.yearCount > 0", new HashMap<String, Object>());
            }
            
        }
        
        return filter;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public Order getOrder() {
        return order;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        String sentence = "FROM Student student WHERE student.disabled = false " + getPreparedFilter(false).getClauseSentence() + order.getSentence();
        return sentence;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).getParameters();
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

}
